package xorg.tools.util;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * Freemarker模板引擎工具类
 * 
 * @author ChenDeWei
 * 
 */
public class FreemarkerUtils {

	private static Configuration cfg = new Configuration();
	private static StringTemplateLoader stringLoader = new StringTemplateLoader();
	
	@SuppressWarnings("unchecked")
	public static String fileFormat(String filename, Map map) throws IOException, TemplateException {
		String path = FreemarkerUtils.class.getResource("/template").getPath();
		cfg.setDirectoryForTemplateLoading(new File(path));  
		Template template = cfg.getTemplate(filename, "utf-8");
		StringWriter writer = new StringWriter();
		template.process(map, writer);

		return writer.toString();

	}


}
